//
//  JJMessageBody.m
//  swiftProject
//
//  Created by yangjian on 2021/12/27.
//

#import "JJMessageBody.h"
#import "JJMessageBigEmojiBody.h"
#import <YYModel.h>



@implementation JJMessageBody


+ (JJMessageBodyType)bodyTypeFromCustomEvent:(NSString *)event {
    if(![event hasPrefix:jjMessageBody_event]) {
        return JJMessageBodyTypeNone;
    }
    NSArray *eventCutting = [event componentsSeparatedByString:jjMessageBody_event_separator];
    if (eventCutting.count < 2) {
        return JJMessageBodyTypeNone;
    }
    JJMessageBodyType type = [eventCutting[1] intValue];
    return type;
}


+ (instancetype)bodyFromEMCustomMessageBody:(EMCustomMessageBody *)customBody{
    JJMessageBody *result = nil;
    JJMessageBodyType type = [self bodyTypeFromCustomEvent:customBody.event];
    switch (type) {
        case JJMessageBodyTypeBigEmoji:
        {
            JJMessageBigEmojiBody *body = JJMessageBigEmojiBody.new;
            body.type = type;
            body.topicid = customBody.customExt[@"topicid"];
            body.identifier = customBody.customExt[@"identifier"];
            body.name = customBody.customExt[@"name"];
            body.isGif = [customBody.customExt[@"isGif"] boolValue];
            result = body;
        }
            break;
        default:
        {
            JJMessageBody *body = JJMessageBody.new;
            body.type = type;
            result = body;
        }
            break;
    }
    return result;

}

- (NSMutableDictionary *)fetchCustomExt{
    NSMutableDictionary *map = NSMutableDictionary.new;
//    map[@"type"] = [NSString stringWithFormat:@"%lu",(unsigned long)self.type];
    return map;
}

- (EMCustomMessageBody *)covertCustomMessageBody{
    NSString *event = [@[jjMessageBody_event,
                         [NSString stringWithFormat:@"%lu",(unsigned long)self.type],
                       ] componentsJoinedByString:jjMessageBody_event_separator];
    
    EMCustomMessageBody *customBody = [[EMCustomMessageBody alloc]
                                       initWithEvent:event
                                       customExt:[NSDictionary dictionaryWithDictionary:self.fetchCustomExt]];
    return customBody;
}



@end
